package com.example.delish.ui.ingredient

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.GridCells
import androidx.compose.foundation.lazy.LazyVerticalGrid
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.primarySurface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.example.delish.R
import com.example.delish.model.IngredientItem
import com.example.delish.ui.recipes.viewmodel.RecipesViewModel
import com.example.delish.ui.theme.DelishComposeTheme
import com.example.delish.ui.theme.compositedOnSurface
import com.google.accompanist.coil.rememberCoilPainter
import com.google.accompanist.imageloading.ImageLoadState

@ExperimentalFoundationApi
@Composable
fun IngredientFullList(
    viewModel: RecipesViewModel,
    onIngredientSearch: (String) -> Unit
) {
    val viewState by viewModel.viewState.collectAsState()
    DelishComposeTheme {
        Scaffold(
            backgroundColor = MaterialTheme.colors.primarySurface,
            topBar = {
                //TODO homeTopBar
            }
        ) {
            Column {
                Text(
                    text = stringResource(id = R.string.ingredients_wikis),
                    style = MaterialTheme.typography.h6,
                    textAlign = TextAlign.Start,
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(16.dp)
                )
                LazyVerticalGrid(cells = GridCells.Adaptive(minSize = 128.dp)) {
                    items(viewState.ingredientList) { ingredientItem ->
                        FullIngredientItem(
                            ingredientItem = ingredientItem,
                            onIngredientSearch = onIngredientSearch
                        )
                    }
                }
            }
        }
    }
}

@Composable
fun FullIngredientItem(
    ingredientItem: IngredientItem,
    onIngredientSearch: (String) -> Unit
) {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier.padding(8.dp)
    ) {
        Box(
            modifier = Modifier
                .clip(RoundedCornerShape(8.dp))
                .background(Color(android.graphics.Color.parseColor(ingredientItem.background)))
                .clickable(onClick = { onIngredientSearch(ingredientItem.title) })
        ) {
            val painter = rememberCoilPainter(request = ingredientItem.image)
            Image(painter = painter, contentDescription = null)
            when(painter.loadState) {
                is ImageLoadState.Loading -> {
                    Box(
                        modifier = Modifier
                            .fillMaxSize()
                            .clip(CircleShape)
                            .background(MaterialTheme.colors.compositedOnSurface(alpha = 0.2f))
                    ) {
                        CircularProgressIndicator()
                    }
                }
                is ImageLoadState.Error -> {

                }
                else -> {

                }
            }
        }
        Text(
            text = ingredientItem.title,
            style = MaterialTheme.typography.caption,
            textAlign = TextAlign.Center,
            modifier = Modifier.padding(top = 4.dp, bottom = 16.dp)
        )
    }
}